package com.common.web;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * Class to be used for formatting JSON responses.
 * 
 * @author Nickevin
 */
public class JsonResponse {

    private static ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally

    private JsonResponse() {}

    public static String generate(boolean flag) {
        return String.format("{ success: %1$s }", flag);
    }

    public static String generate(boolean flag, String msg) {
        return String.format("{ success: %1$s,\r msg: '%2$s' }", flag, msg);
    }

    public static String generate(Object bean) throws JsonGenerationException,
            JsonMappingException, IOException {
        return String.format("{ success: %1$s,\r data: '%2$s' }", true, mapper
                .writeValueAsString(bean));
    }

    public static Json write(boolean flag) {
        return new Json(flag);
    }

    public static Json write(boolean flag, String message) {
        return new Json(flag, message);
    }

    public static Json write(Object obj) {
        return new Json(obj);
    }
}
